﻿using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;
using System.Text;
using System.IO;

namespace 默写
{
    public class ValidateString
    {
        private const string validatebook = @"^([0-6]{1}[\uff1a:]{1}[\u4e0a\u4e0b]{1}\u518c[\uff1a:](\u8bc6\u5b57|\u8bfe\u6587)[\uff1a:]{1}[0-9]+[\uff1a:]{1}[\u8ba4\u5199]{1}[\uff1a:]{1}[\u4e00-\u9fa5]{1}([\uff1a:][\u4e00-\u9fa5]+([\uff1b;]{1}[\u4e00-\u9fa5]+)*)?)?$";
        private const string validatelesson = @"^((\u8bc6\u5b57|\u8bfe\u6587)[\uff1a:]{1}[0-9]+[\uff1a:]{1}[\u8ba4\u5199]{1}[\uff1a:]{1}[\u4e00-\u9fa5]{1}([\uff1a:][\u4e00-\u9fa5]+([\uff1b;]{1}[\u4e00-\u9fa5]+)*)?)?$";
        private const string validatechars = @"^([\u8ba4\u5199]{1}[\uff1a:]{1}[\u4e00-\u9fa5]{1}([\uff1a:][\u4e00-\u9fa5]+([\uff1b;]{1}[\u4e00-\u9fa5]+)*)?)?$";
        private const string validatewords = @"^[\u4e00-\u9fa5]+([\uff1b;\n]{1}[\u4e00-\u9fa5]+)*$";

        /// <summary>
        /// 测试词组字符串是否正确
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateWordsString(params string[] str)
        {
            Regex rx = new Regex(validatewords);
            foreach (string astr in str) {
                if (!rx.IsMatch(astr)) return false;
            }
            return true;
        }
        public static bool ValidateWordsString(RichTextBox richTextBox)
        {
            int line = 0;
            bool rtnvalue = true;
            while (true)
            {

                int a = richTextBox.GetFirstCharIndexFromLine(line);
                int b = richTextBox.GetFirstCharIndexFromLine(++line);
                if (a == -1)
                    return rtnvalue;
                else if (b == -1)
                    b = richTextBox.TextLength - a;
                else
                    b = b - a;
                richTextBox.Select(a, b);
                if (!ValidateString.ValidateWordsString(richTextBox.SelectedText.Replace(" ", "")))
                {
                    richTextBox.SelectionColor = Color.Red;
                    rtnvalue = false;
                }
                else
                {
                    richTextBox.SelectionColor = Color.Black;
                }
            }
        }

        /// <summary>
        /// 测试书本字符串是否正确
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateBookString(params string[] str)
        {
            Regex rx = new Regex(validatebook);
            foreach (string astr in str)
            {
                if (!rx.IsMatch(astr)) return false;
            }
            return true;
        }
        public static bool ValidateBookString(RichTextBox richTextBox)
        {
            int line = 0;
            bool rtnvalue = true;
            while (true)
            {

                int a = richTextBox.GetFirstCharIndexFromLine(line);
                int b = richTextBox.GetFirstCharIndexFromLine(++line);
                if (a == -1)
                    return rtnvalue;
                else if (b == -1)
                    b = richTextBox.TextLength - a;
                else
                    b = b - a;
                richTextBox.Select(a, b);
                if (!ValidateString.ValidateBookString(richTextBox.SelectedText.Replace(" ", "")))
                {
                    richTextBox.SelectionColor = Color.Red;
                    rtnvalue = false;
                }
                else
                {
                    richTextBox.SelectionColor = Color.Black;
                }
            }
        }

        /// <summary>
        /// 测试课文字符串是否正确
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateLessonString(params string[] str)
        {
            Regex rx = new Regex(validatelesson);
            foreach (string astr in str)
            {
                if (!rx.IsMatch(astr)) return false;
            }
            return true;
        }
        public static bool ValidateLessonString(RichTextBox richTextBox)
        {
            int line = 0;
            bool rtnvalue = true;
            while (true)
            {

                int a = richTextBox.GetFirstCharIndexFromLine(line);
                int b = richTextBox.GetFirstCharIndexFromLine(++line);
                if (a == -1)
                    return rtnvalue;
                else if (b == -1)
                    b = richTextBox.TextLength - a;
                else
                    b = b - a;
                richTextBox.Select(a, b);
                if (!ValidateString.ValidateLessonString(richTextBox.SelectedText.Replace(" ", "")))
                {
                    richTextBox.SelectionColor = Color.Red;
                    rtnvalue = false;
                }
                else
                {
                    richTextBox.SelectionColor = Color.Black;
                }
            }
        }

        /// <summary>
        /// 测试字符字符串是否正确
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool ValidateCharString(params string[] str)
        {
            Regex rx = new Regex(validatechars);
            foreach (string astr in str)
            {
                if (!rx.IsMatch(astr)) return false;
            }
            return true;
        }
        public static bool ValidateCharString(RichTextBox richTextBox)
        {
            int line = 0;
            bool rtnvalue = true;
            while (true)
            {

                int a = richTextBox.GetFirstCharIndexFromLine(line);
                int b = richTextBox.GetFirstCharIndexFromLine(++line);
                if (a == -1)
                    return rtnvalue;
                else if (b == -1)
                    b = richTextBox.TextLength - a;
                else
                    b = b - a;
                richTextBox.Select(a, b);
                if (!ValidateString.ValidateCharString(richTextBox.SelectedText.Replace(" ", "")))
                {
                    richTextBox.SelectionColor = Color.Red;
                    rtnvalue = false;
                }
                else
                {
                    richTextBox.SelectionColor = Color.Black;
                }
            }
        }
    }
}
